package com.tedu.activity.service;

import com.tedu.activity.dao.ActivityDao;
import com.tedu.activity.pojo.Activity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * <p>
 *
 * </p>
 *
 * @author Song
 * @since 2020/10/9
 */
@Service
@Slf4j
public class ActivityServiceImpl implements ActivityService{

    @Autowired
    ActivityDao activityDao;

    @Override
    public List<Activity> findActivitys() {
        log.info("---------------");
        return activityDao.findActivitys();
    }

    @Override
    public int saveActivity(Activity activity) {
        int rows=activityDao.insertObject(activity);
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //一旦调用此任务的线程获得了CPU就会执行这个任务的run方法
                activityDao.updateState(activity.getId());
                timer.cancel();
            }
        }, activity.getEndTime());
        return rows;
    }

    @Override
    public int updateState(Long id) {
        return activityDao.updateState(id);
    }

    @Override
    public int deleteObject(Long id) {
        return activityDao.deleteObject(id);
    }
}
